package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.CollegeDAO;
import cn.herlioner.pojo.College;
import cn.herlioner.pojo.TSObject;

public class CollegeDAOImpl implements CollegeDAO{

	@Override
	public void add(College c) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.save(c);
		
		s.getTransaction().commit();
		s.close();
		sf.close();
		
	}

	@Override
	public College get(Long long1) {
		College result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (College) s.get(College.class, long1);
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(Long clgnum) {
		SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        College p = (College) s.get(College.class, clgnum);
 
        s.delete(p);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
		
	}

	@Override
	public void update(College c) {
		SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        s.update(c);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
		
	}

	@Override
	public List<College> list() {
		List<College> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from College c");
 
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}

}
